<?php
    abstract class functions {
        
        function show($name){
               // echo 'ddd S_Show' ;
                $path = __SITE_PATH.'/views/'.$name.'.php'; //lấy đường dẫn và tên của file cần show
                
                if(file_exists($path) ==false){
                    throw new Exception('Khong tim thay file '. $name .' trong folder '.$path);
                    return false;
                }
                include ($path) ; // goi tới file càn hiển thị
        }
        
        /**
         * Ham dung javacript de thong bao su kien
         * @param String message
         * @param $pageReturn : trang ma trinh duyet se hien thi sau khi process
         * @source file : m_show.php
         * @return void
         * 
         **/             
         function jMessage($mess,$pageReturn=null){
            if($pageReturn!=null){
                echo '<script type="text/javascript"> alert("'.$mess.'");
                        window.location = "index.php?rt='.$pageReturn.'";
                    </script> '; //dinh dang trang se tra ve sau khi process
            }else{
                echo '<script type="text/javascript"> alert("'.$mess.'");
                    </script> ';
            }
            return false;
         }
         
         /**
          * Dem so lan truy cap cua trang web
          * @return int
          * */
         public function demSoLanTruyCap(){
            $file = fopen("upload/files/count.ini","r");			
        	$count=fgets($file);
        	fclose($file);
        	$count=(int)$count+1;
        	$file=fopen("upload/files/count.ini","w");
        	fwrite($file,$count);
        	fclose($file);
        	echo $count;
         }
             
             /**
    	 * Ham fan tran
    	 * @param $total: tổng số mẫu tin
    	 * @param $currentPage: trang hiện hành
    	 * @param $div: số trang trong 1 đoạn
    	 * @param $rows: số dòng trên 1 trang
    	 * @param $pageLinkTo (String): dinh dang trang se dc goi
    	 */
        function divPage($total = 0,$currentPage = 0,$div = 3,$rows = 10, $rt=null, $pageLinkTo){
            //kiem tra thu bien rt co null hay ko
            
            if($rt!=null){
                $rt = "rt=".$rt;
            }else{
                $rt="";
            }
            if(!$total || !$rows || !$div || $total<=$rows) return false;
            $nPage = floor($total/$rows) + (($total%$rows)? 1 : 0);
            $nDiv = floor($nPage/$div) + (($nPage%$div)?1:0);
            $currentDiv = floor($currentPage/$div) ; //tac dung lam tron xuong (vd 3.99 -> 3)
            $sPage = '<ul class="breakPage">';
            if($currentDiv) {
                $sPage .= '<li><a href="./?'.$rt.$pageLinkTo.'&p=1"><<</a></li> ';
                $sPage .= '<li><a href="./?'.$rt.$pageLinkTo.'&p='.($currentDiv*$div).'"><</a></li> ';
            }            
            $count =($nPage<=($currentDiv+1)*$div)?($nPage-$currentDiv*$div):$div;
            for($i=0;$i<$count;$i++){
                $page = ($currentDiv*$div + $i);
                
                //if(($currentDiv) > ($nDiv-2) && $currentDiv ){
                  //  $sPage .= '<li><a href="./?'.$rt.$pageLinkTo.'&p='.($currentDiv*$div + $i).'" '.(($page==$currentPage)?'class="current"':'').'>'.($page).'</a> </li>';
                //}else{
                    $sPage .= '<li><a href="./?'.$rt.$pageLinkTo.'&p='.($currentDiv*$div+1 + $i).'" '.(($page==$currentPage)?'class="current"':'').'>'.($page+1).'</a> </li>';
                //}
            }
            if($currentDiv < $nDiv - 2){
                $sPage .= '<li><a href="./?'.$rt.$pageLinkTo.'&p='.(($currentDiv+1)*$div ).'">></a> </li>';
                $sPage .= '<li><a href="./?'.$rt.$pageLinkTo.'&p='.(($nDiv-1)*$div).'">>></a></li>';
            }
            
            $sPage.='</ul>';
            return $sPage;
        }
        
        /**
         * Lay ten trinh duyet
         * @return String;
         * */
        function get_user_browser(){ 
            $u_agent = $_SERVER['HTTP_USER_AGENT']; 
            $ub = ''; 
            if(preg_match('/MSIE/i',$u_agent)) 
            { 
                $ub = "ie"; 
            } 
            elseif(preg_match('/Firefox/i',$u_agent)) 
            { 
                $ub = "firefox"; 
            } 
            elseif(preg_match('/Safari/i',$u_agent)) 
            { 
                $ub = "safari"; 
            } 
            elseif(preg_match('/Chrome/i',$u_agent)) 
            { 
                $ub = "chrome"; 
            } 
            elseif(preg_match('/Flock/i',$u_agent)) 
            { 
                $ub = "flock"; 
            } 
            elseif(preg_match('/Opera/i',$u_agent)) 
            { 
                $ub = "opera"; 
            } 
            
            return $ub; 
        }
        
        function currentPageURL() {
            $pageURL = 'http';
            if ($_SERVER["HTTPS"] == "on") {
                $pageURL .= "s";
            }
            $pageURL .= "://";
            if ($_SERVER["SERVER_PORT"] != "80") {
                $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
            } else {
                $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
            }
            return $pageURL;
        }
        
        function getItemUrl($url){
            $part = explode('?',$url);
            $item = $part[1];
            return $item;
        }     
         
       
    }
?>